/*  This file is part of Phantom Power.
 *  Copyright (C) 2010 Jacob Dawid
 *
 *  Phantom Power is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Phantom Power is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Phantom Power. If not, see <http://www.gnu.org/licenses/>.
 */

#include "mainDialog.h"

mainDialog::mainDialog()
{
	settings=new QSettings("phantom-power.configuration", QSettings::IniFormat);
	
	currentDevice=new device();
	connect(currentDevice, SIGNAL(report(QString)), this, SLOT(handleReport(QString)));

	setupUserInterface();
}

mainDialog::~mainDialog()
{
	delete currentDevice;
	delete _wifiDialog;
	delete _usbDialog;
	delete settings;
}

void mainDialog::setupUserInterface()
{
	setWindowTitle("Phantom Power 0.5 Client");

	setCentralWidget(new QWidget());

	connectionTypeSelect=new QTabWidget(centralWidget());
	connectionTypeSelect->setGeometry(10, 10, 280, 140);
	connectionTypeSelect->insertTab(0, _wifiDialog=new wifiDialog(), "WiFi");
	connectionTypeSelect->insertTab(1, _usbDialog=new usbDialog(), "USB");

	console=new QTextEdit(centralWidget());
	console->setGeometry(10, 160, 280, 230);
	console->setReadOnly(true);
	console->append("Ready.");

	deviceTypeSelect=new QTabWidget(centralWidget());
	deviceTypeSelect->setGeometry(300, 10, 290, 380);
	deviceTypeSelect->insertTab(0, mouseSettings=new QWidget(), "Mouse");
	deviceTypeSelect->insertTab(1, keyboardSettings=new QWidget(), "Keyboard");
	deviceTypeSelect->insertTab(2, sliderSettings=new QWidget(), "Slider");
	deviceTypeSelect->insertTab(3, gamepadSettings=new QWidget(), "Gamepad");


	// TODO: if it gets too big, refactor it to a mouseDialog
	QCheckBox* reverseX=new QCheckBox(mouseSettings);
	reverseX->setGeometry(10, 10, 20, 20);
	QCheckBox* reverseY=new QCheckBox(mouseSettings);
	reverseY->setGeometry(10, 40, 20, 20);
	QSlider* speedX=new QSlider(Qt::Horizontal, mouseSettings);
	speedX->setGeometry(10, 95, 250, 20);
	speedX->setRange(0, 1000);
	QSlider* speedY=new QSlider(Qt::Horizontal, mouseSettings);
	speedY->setGeometry(10, 145, 250, 20);
	speedY->setRange(0, 1000);

	QLabel* label=new QLabel(mouseSettings);
	label->setText("Reverse X mouse axis");
	label->setGeometry(30, 10, 250, 20);
	label=new QLabel(mouseSettings);
	label->setText("Reverse Y mouse axis");
	label->setGeometry(30, 40, 250, 20);
	label=new QLabel(mouseSettings);
	label->setText("Adjust X axis speed");
	label->setGeometry(10, 70, 250, 20);
	label=new QLabel(mouseSettings);
	label->setText("Adjust Y axis speed");
	label->setGeometry(10, 120, 250, 20);

	reverseX->setChecked(settings->value("mouse/reverseX").toBool());
	reverseY->setChecked(settings->value("mouse/reverseY").toBool());	
	speedX->setValue(settings->value("mouse/speedX").toInt());
	speedY->setValue(settings->value("mouse/speedY").toInt());

	connect(reverseX, SIGNAL(stateChanged(int)), this, SLOT(slotReverseX(int)));
	connect(reverseY, SIGNAL(stateChanged(int)), this, SLOT(slotReverseY(int)));
	connect(speedX, SIGNAL(valueChanged(int)), this, SLOT(slotSpeedX(int)));
	connect(speedY, SIGNAL(valueChanged(int)), this, SLOT(slotSpeedY(int)));
	// -end mouse


	connect(_wifiDialog, SIGNAL(establishConnection(int,int,int,int,int)), this, SLOT(establishWiFiConnection(int,int,int,int,int)));
	connect(_wifiDialog, SIGNAL(cutConnection()), this, SLOT(cutWiFiConnection()));
	
	connect(this, SIGNAL(synchronisation()), _wifiDialog, SLOT(synchronize()));
	connect(this, SIGNAL(synchronisation()), _usbDialog, SLOT(synchronize()));
	connect(this, SIGNAL(synchronisation()), currentDevice, SLOT(synchronize()));
}


void mainDialog::handleReport(QString message)
{
	// This slot handles all incoming messages.
	console->append(message);
}

void mainDialog::establishWiFiConnection(int ip1, int ip2, int ip3, int ip4, int port)
{
	// Generate a new connection. Theoretically, several connection are allowed simultaneously, although
	// the current implementation only supports one at a time.
	currentConnection=new connection();

	// In order to receive issues (ie. changes at the mobile device) and status reports, we have to connect
	// ourselves listening to the corresponding signals.
	connect(currentConnection, SIGNAL(issueAvailable(QList<double>)), currentDevice, SLOT(handleIssue(QList<double>)));
	connect(currentConnection, SIGNAL(controlSignal(QString)), currentDevice, SLOT(handleControlSignal(QString)));
	connect(currentConnection, SIGNAL(report(QString)), this, SLOT(handleReport(QString)));
	connect(this, SIGNAL(synchronisation()), currentConnection, SLOT(synchronize()));
	
	currentConnection->start(ip1, ip2, ip3, ip4, port);
}

void mainDialog::cutWiFiConnection()
{
	delete currentConnection;
}

void mainDialog::synchronize()
{
	settings->sync();
	emit synchronisation();
}

void mainDialog::slotReverseX(int state)
{
	settings->setValue("mouse/reverseX", (bool)state);
	synchronize();
}

void mainDialog::slotReverseY(int state)
{
	settings->setValue("mouse/reverseY", (bool)state);
	synchronize();
}

void mainDialog::slotSpeedX(int state)
{
	settings->setValue("mouse/speedX", state);
	synchronize();
}

void mainDialog::slotSpeedY(int state)
{
	settings->setValue("mouse/speedY", state);
	synchronize();
}
